package com.hxd.otc.module.home.presenter;

import com.hxd.otc.R;
import com.hxd.otc.api.Constant;
import com.hxd.otc.base.BaseRecyclerPresenter;
import com.hxd.otc.module.home.adapter.TicketDetailAdapter;
import com.hxd.otc.module.home.contract.TicketDetailContract;
import com.hxd.otc.utils.GsonUtil;
import com.hxd.otc.utils.ResourceUtil;
import com.hyphenate.chat.ChatClient;
import com.hyphenate.helpdesk.callback.ValueCallBack;
import com.hyphenate.helpdesk.domain.CommentEntity;
import com.hyphenate.helpdesk.domain.CommentListResponse;
import com.hyphenate.helpdesk.domain.TicketEntity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * Created by lzy on 2019/1/16 0016.
 * description ${留言详情Presenter}
 */
public class TicketDetailPresenter extends BaseRecyclerPresenter<TicketDetailContract.IView, TicketDetailAdapter, CommentEntity> implements TicketDetailContract.IPresenter {

    private TicketEntity mTicketEntity;

    public TicketDetailPresenter(TicketDetailContract.IView iView) {
        super(iView);
    }

    @Override
    public void loadData(boolean isRefresh) {
        if (mTicketEntity == null) {
            onLoadDataFail(isRefresh, "");
            return;
        }
        loadAllComments(isRefresh, mTicketEntity.getId());
    }

    @Override
    public void setTicketEntity(TicketEntity ticketEntity) {
        mTicketEntity = ticketEntity;
    }

    @Override
    public TicketEntity getTicketEntity() {
        return mTicketEntity;
    }

    /**
     * 加载留言的所有评论
     *
     * @param isRefresh
     * @param ticketId  留言的ID
     */
    private void loadAllComments(final boolean isRefresh, String ticketId) {
        if (!ChatClient.getInstance().isLoggedInBefore()) {
            onLoadDataFail(isRefresh, ResourceUtil.getString(R.string.login_user_noti));
            return;
        }
        String target = Constant.HyphenateKeFu.DEFAULT_CUSTOMER_ACCOUNT;
        String tenantId = Constant.HyphenateKeFu.DEFAULT_TENANT_ID;
        String projectId = Constant.HyphenateKeFu.DEFAULT_PROJECT_ID;

        ChatClient.getInstance().leaveMsgManager().getLeaveMsgComments(projectId, ticketId, target, new ValueCallBack<String>() {

            @Override
            public void onSuccess(final String value) {
                if (mView == null) return;
                mView.runOnUi(new Runnable() {
                    @Override
                    public void run() {
                        CommentListResponse commentListResponse = GsonUtil.fromJson(value, CommentListResponse.class);

                        ArrayList<CommentEntity> commentEntities = new ArrayList<>();
                        if (commentListResponse != null && commentListResponse.getSize() != 0) {
                            commentEntities.addAll(commentListResponse.getEntities());
                        }

                        Collections.sort(commentEntities, new Comparator<CommentEntity>() {
                            @Override
                            public int compare(CommentEntity lhs, CommentEntity rhs) {
                                return lhs.getCreated_at().compareTo(rhs.getCreated_at());
                            }
                        });

                        if (mView == null) return;
                        onLoadDataSuccess(isRefresh, commentEntities);
                    }
                });

            }

            @Override
            public void onError(int error, final String errorMsg) {
                if (mView == null) return;
                mView.runOnUi(new Runnable() {
                    @Override
                    public void run() {
                        onLoadDataFail(isRefresh, ResourceUtil.getString(R.string.comment_load_fail));
                    }
                });
            }
        }, 0, 100);
    }
}
